package api

import (
	"flag"
	"github.com/gin-gonic/gin"
	pb "user_srv/proto"
	"week2_api/internal/api/request"
	"week2_api/internal/api/response"
	"week2_api/internal/handler"
)

var (
	userServer = flag.String("UserServer", "127.0.0.1:50051", "The server address")
	userSrv    = handler.NewClient().UserServer(userServer)
)

func Register(c *gin.Context) {
	var json *request.User
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}
	register, err := userSrv.Register(c, &pb.RegisterRequest{
		Name:      json.Name,
		Image:     json.Image,
		Where:     int64(json.Where),
		ObjRoom:   int64(json.ObjRoom),
		Job:       int64(json.Job),
		Title:     int64(json.Title),
		NumPrice:  float32(json.NumPrice),
		LookPrice: float32(json.LookPrice),
		Cont:      json.Cont,
	})
	if err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}
	response.Response(200, "医生注册成功", register, c)
}

func Find(c *gin.Context) {
	var json *request.Find
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}

	find, err := userSrv.Find(c, &pb.FindRequest{KeyWord: json.KeyWord})

	if err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}
	response.Response(200, "医生注册成功", find, c)

}
